﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Workflow.Activities;
using System.Workflow.ComponentModel;
using SharedWorkflows;
using System.Workflow.Runtime.Hosting;
using CommonLibrary.DataModel.Basic;

namespace WinformTest
{
    public partial class Boss : Form
    {
        public Boss()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            using (DataTable dt = GlobleVar.GetAllWorkflowTable())
            {
                //dt.DefaultView.RowFilter = "[当前状态] = 'stateBoss'";
                if(dt!=null)
                    this.dataGridView1.DataSource = dt.DefaultView;
            }
            base.OnLoad(e);
        }

        #region UIevent

        private void button1_Click(object sender, EventArgs e)
        {
            string s = txtGuid.Text.Trim();
            if (s.Length > 0 && comboBox1.SelectedValue.ToString() != "-1")
            {
                string nid = comboBox1.SelectedValue.ToString();
                Guid instanceId = new Guid(s);

                BillServer billserver = (GlobleVar._workflowManager.WorkflowRuntime).GetService<BillServer>();
                ManualWorkflowSchedulerService scheduler = 
                    (GlobleVar._workflowManager.WorkflowRuntime).GetService<ManualWorkflowSchedulerService>();

                StateMachineWorkflowInstance stateInstance = 
                    new StateMachineWorkflowInstance(GlobleVar._workflowManager.WorkflowRuntime, instanceId);
                EventDrivenActivity edact = (EventDrivenActivity)stateInstance.CurrentState.Activities[nid];
                HandleExternalEventActivity heva = (HandleExternalEventActivity)edact.EnabledActivities[0];

                BillModel model = new BillModel();

                model.LastRemark = model.Bremark = string.Concat("Boos: ", comboBox1.SelectedText, ":", txtRemark.Text);
                //billserver.RaiseEvent(heva.EventName, instanceId);

                scheduler.RunWorkflow(instanceId);
                comboBox1.Items.Clear();
                txtGuid.Text = "";
                //FileTools.XmlSerializeObject(wsPath, instanceId, model);
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtGuid.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
            btnReadGuid_Click(btnReadGuid, null);
        }

        private void btnReadGuid_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtGuid.Text)) return;
            string s = txtGuid.Text.Trim();
            if (s.Length > 0)
            {
                Guid instanceId = new Guid(s);
                StateMachineWorkflowInstance stateInstance = 
                    new StateMachineWorkflowInstance(
                        GlobleVar._workflowManager.WorkflowRuntime, instanceId);
                using (DataTable dt = new DataTable())
                {
                    dt.Columns.Add("NID", typeof(string));
                    dt.Columns.Add("NTEXT", typeof(string));
                    dt.Rows.Add("-1", "==请选择==");

                    int i = 0;
                    foreach (Activity act in stateInstance.CurrentState.EnabledActivities)
                    {

                        if (act is EventDrivenActivity)
                        {
                            EventDrivenActivity edact = (EventDrivenActivity)act;

                            if (edact.EnabledActivities.Count > 0 && edact.EnabledActivities[0] is HandleExternalEventActivity)
                            {
                                dt.Rows.Add(edact.Name, act.Description);
                            }
                        }
                        i++;
                    }
                    comboBox1.ValueMember = "NID";
                    comboBox1.DisplayMember = "NTEXT";
                    comboBox1.DataSource = dt;
                }
            }
        }

        #endregion
    }
}
